package com.cy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cy.bfzcommons.mapper.RightsMapper;
import com.cy.bfzcommons.mapper.RoleRightsMapper;
import com.cy.bfzcommons.pojo.Rights;
import com.cy.bfzcommons.pojo.RoleRight;
import com.cy.bfzcommons.service.RightsService;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RightsServiceImpl implements RightsService {
    @Autowired
    private RightsMapper rightsMapper;

    @Autowired
    private RoleRightsMapper roleRightsMapper;

    public Map<Integer,List<Rights>> getMap(){
        Map<Integer,List<Rights>> map = new HashMap<>();
        //1.查询所有的数据信息
        List<Rights> list =  rightsMapper.selectList(null);
        //2.将List集合封装到Map中
        for (Rights right : list ){
            //规则:  判断map中是否有key
            // 没有key???  该子级是第一个父级元素的孩子,应该声明父级并且将子级作为第一个子级保存
            // 有key??  找到父级的子级序列  将子级追加到序列中即可
            if (map.containsKey(right.getParentId())){
                //获取父级的所有已知子级
                map.get(right.getParentId()).add(right);


            }else {
                //没有父级
                List<Rights> initList = new ArrayList<>();
                initList.add(right);
                map.put(right.getParentId(),initList);
            }

        }
        return map;
    }




    @Override
    public List<Rights> getRights() {
        int level = 2;
        if (level>1){
            QueryWrapper<Rights> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id",0);
            List<Rights> list = rightsMapper.selectList(queryWrapper);
            for (Rights rights : list){
                QueryWrapper<Rights> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("parent_id",rights.getId());
                List<Rights> list1 = rightsMapper.selectList(queryWrapper1);
                rights.setChildren(list1);
            }
            return list;
        }

        QueryWrapper<Rights> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parentId",0);


        return rightsMapper.selectList(queryWrapper);
    }

    @Override
    public List<Rights> selectFind(Integer level) {
        Map<Integer,List<Rights>> map = getMap();
        List<Rights> catList = new ArrayList<>();
        if (level ==1){
            //获取一级商品分类信息
            return  map.get(0);
        }

        if (level == 2){

            return getLevel2(map);
        }

        if (level == 3){
            return  getLevel3(map);
        }
        return getLevel3(map);
    }

    @Override
    public String insertRights(Rights rights) {
        String name = rights.getName();
        if (StringUtil.isEmpty(name))return "新增失败,请将信息填写完整";
        rightsMapper.insert(rights);
        return "新增权限成功";
    }

    @Override
    public String updateRights(Rights rights) {
        if (StringUtil.isEmpty(rights.getName()))return "名字不能为空";
        rightsMapper.updateById(rights);
        return "修改成功";
    }

    //封装删除权限表与菜单表关联的方法
    public void deleteRightsRole(Integer id){
        QueryWrapper<RoleRight> queryWrapper = new QueryWrapper<>();
        //删除权限表与菜单表的关联
        queryWrapper.eq("rights_id", id);
        roleRightsMapper.delete(queryWrapper);
    }

    @Override
    public void deleteById(Integer id,Integer level) {
        if (level==3){
            rightsMapper.deleteById(id);
            QueryWrapper<RoleRight> queryWrapper = new QueryWrapper<>();
            //删除权限表与菜单表的关联
            queryWrapper.eq("rights_id", id);
            roleRightsMapper.delete(queryWrapper);

        }

        if (level == 2){
            QueryWrapper<RoleRight> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("rights_id", id);
            roleRightsMapper.delete(queryWrapper1);


            QueryWrapper<Rights> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", id).select("id");
            List<Rights> ids =  rightsMapper.selectList(queryWrapper);
            for (Rights rights:ids){
                deleteRightsRole(rights.getId());
            }

            rightsMapper.delete(queryWrapper);
            rightsMapper.deleteById(id);
        }

        if (level == 1){
            QueryWrapper<Rights> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", id).select("id");
            deleteRightsRole(id);


            List<Rights> list = rightsMapper.selectList(queryWrapper);
            for (Rights rights : list){
                deleteRightsRole(rights.getId());

                QueryWrapper<Rights> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("parent_id", rights.getId()).select("id");
                List<Rights> ids =  rightsMapper.selectList(queryWrapper2);
                for (Rights rights1:ids){
                    deleteRightsRole(rights1.getId());
                }

                QueryWrapper<Rights> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("parent_id", rights.getId());
                rightsMapper.delete(queryWrapper1);

            }
            rightsMapper.delete(queryWrapper);
            rightsMapper.deleteById(id);

        }
    }

    @Override
    public List<Rights> selectRightsByName(String username) {
        Set<String> usernames = rightsMapper.selectRightsByName(username);
        List<Rights> list = selectFind(2);
//        for (Rights rights : list){
//            if(!usernames.contains(rights.getName())){
//                list.remove(rights);
//            }
//            for (Rights rights1 : rights.getChildren()){
//                if (!usernames.contains(rights1.getName())){
//                    rights.getChildren().remove(rights1);
//                }
//            }
//
//        }

        for (int i=0;i<list.size();i++) {
            if (!usernames.contains(list.get(i).getName())) {
                list.remove(i);
                //删除之后索引-1
                if (i != 0) {
                    i--;
                }
            }
            if (list.get(i).getChildren() != null) {
                for (int j = 0; j < list.get(i).getChildren().size(); j++) {
                    System.out.println("size:" + list.get(i).getChildren().size() + ":" + i);
                    System.out.println(list.get(i).getChildren().get(j).getName());
                    if (!usernames.contains(list.get(i).getChildren().get(j).getName())) {
                        System.out.println("进来过吗?/");
                        list.get(i).getChildren().remove(j);

                        if (j != 0) {
                            j = j - 1;
                        }
                    }
                }
            }
        }
        return list;
    }

    private List<Rights> getLevel3(Map<Integer, List<Rights>> map) {
        //1.获取二级商品分类信息 1级里面套二级
        List<Rights> list =  getLevel2(map);
        for (Rights right : list){
            List<Rights> list2 = right.getChildren();
            //根据2级查询3级信息
            if (list2 == null) continue;  //判断数据是否有效!
            for (Rights right2:list2){
                right2.setChildren(map.get(right2.getId()));
            }
            //将3级已经封装完成  将二级封装到一级中
            right.setChildren(list2);
        }
        return list;
    }

    private List<Rights> getLevel2(Map<Integer, List<Rights>> map) {
        //1.获取1级商品分类信息
        List<Rights> list = map.get(0);
        //2.封装2级菜单信息
        for (Rights right : list){
            //如何获取该一级菜单的子级?
            right.setChildren(map.get(right.getId()));

        }
        return list;
    }


}
